package com.bardsoftware.foronuvolo.server;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import com.bardsoftware.foronuvolo.data.DatastoreDiscussionChunk;
import com.bardsoftware.foronuvolo.data.DiscussionChunk;
import com.bardsoftware.foronuvolo.data.ForumUser;
import com.bardsoftware.foronuvolo.data.DiscussionsViewItem;
import com.bardsoftware.foronuvolo.data.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.repackaged.com.google.common.base.Pair;
import com.google.appengine.repackaged.com.google.common.collect.Lists;

abstract class DiscussionsViewServlet extends HttpServlet {
    private static final Logger LOGGER = Logger.getLogger(DiscussionsViewServlet.class.getName());
    private final String myJspFilename;
    private final Map<String, String> myAdditionalAttributes;

    public DiscussionsViewServlet(String jspFilename, Map<String,String> additionalAttributes) {
        myJspFilename = JspHelper.getJspName(jspFilename);
        myAdditionalAttributes = additionalAttributes;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if (!req.getRequestURL().toString().startsWith(ForoNuvoloConstants.FORUM_DOMAIN)) {
            return;
        }

        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(myJspFilename);
        ForumUser user = com.bardsoftware.foronuvolo.server.UserService.getUser(req);
        req.setAttribute("nickname", user.getNickname());
        if (user != ForumUser.ANONYMOUS) {
            req.setAttribute("user_id", user.getID());
        }
        DiscussionChunk chunk = getPage(req);
        if (chunk == null) {
            chunk = DatastoreDiscussionChunk.getFirstPage();
            LOGGER.warning("Failed to find chunk for request="
                    + req.getRequestURL().append('?').append(req.getQueryString()).toString());
        }
        DiscussionsViewPage page = DiscussionsViewPage.getPage(chunk);
        req.setAttribute("discussions", page.getItems());
        req.setAttribute("nextPage", page.getNextPageID());
        req.setAttribute("prevPage", page.getPrevPageID());
        req.setAttribute("namespace", ForoNuvoloConstants.PAGE_PATH);
        req.setAttribute("friendConnectID", ForoNuvoloConstants.FRIEND_CONNECT_SITE_ID);

        req.setAttribute("redirectUrl", req.getRequestURL().append("?page=").append(chunk.getID()));
        req.setAttribute("date", new Date());
        if (req.getHeader("User-Agent").contains("Opera")) {
            req.setAttribute("opera", "true");
        }

        for (Map.Entry<String, String> attrs : myAdditionalAttributes.entrySet()) {
            req.setAttribute(attrs.getKey(), attrs.getValue());
        }
        dispatcher.forward(req, resp);
    }

    protected abstract DiscussionChunk getPage(HttpServletRequest req);
}
